<?php

namespace Apps\AppApi\Method\Note;

use Apps\AppApi\Method\BaseMethod;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\Inorout;
use Phalcon\Mvc\Model;


/**
 * 收支分类列表
 * @author      545
 * @date        2016年3月4日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class InoroutList extends BaseMethod
{
    
    private $success = 0;
    private $id = 0;

    public function run()
    {
        //验证是否登录
        if(false == $this->verifyUserAuth()){
            return false;
        }
        
        $userid = $this->getUserAuth()->userId;
        
        $type = $this->getDataItem('type',1);
    	
    	$data = Inorout::query()
        ->columns(['inorout_id', 'inorout_title'])
        ->where('state = 1 AND parent_id = 0 AND user_id = :uid: AND type = :type:')
        ->bind(['uid'=>$userid,'type'=>$type])
        ->orderBy('inorout_id DESC')
        ->execute()
        ->toArray();
    	//如果值为空 添加默认数据
    	if (empty($data)&&$type==1){
    	    $default = ['储值卡','疗程卡','项目','家居产品'];

    	    foreach ($default as $k=>$v){

    	        $inorout = new Inorout();
    	        $temp['inorout_title'] = $v;
    	        $temp['createtime'] = date("Y-m-d H:i:s", time());
    	        $temp['user_id'] = $userid;
    	        $temp['type'] = $type;
    	        
    	        $inorout->save($temp);
    	    }
    	    
    	    $data = Inorout::query()
    	    ->columns(['inorout_id', 'inorout_title'])
    	    ->where('state = 1 AND parent_id = 0 AND user_id = :uid: AND type = :type:')
    	    ->bind(['uid'=>$userid,'type'=>$type])
    	    ->orderBy('inorout_id DESC')
    	    ->execute()
    	    ->toArray();
    	}
        
        foreach ($data as $k=>$v){
            $list[$k]['inorout_id'] = $v['inorout_id'];
            $list[$k]['inorout_title'] = $v['inorout_title'];
            if ($type==1){
                $list[$k]['childtitle'] = Inorout::query()
                                        ->columns(['inorout_id', 'inorout_title'])
                                        ->where('state = 1 AND parent_id = :id: AND user_id = :uid:')
                                        ->bind(['id' => $v['inorout_id'],'uid'=>$userid])
                                        ->orderBy('inorout_id DESC')
                                        ->execute()
                                        ->toArray();
            }
         }
        
        $this->setResult($list);
        
    }

}